package com.xidian.zhiancloud.controller.monitor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xidian.zhiancloud.configuration.MonitorConfig;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.MonitorDevice;
import com.xidian.zhiancloud.model.MonitorDeviceTag;
import com.xidian.zhiancloud.qo.monitor.MonitorDeviceQO;
import com.xidian.zhiancloud.service.monitor.MonitorDeviceService;
import com.xidian.zhiancloud.service.monitor.MonitorDeviceTagService;
import com.xidian.zhiancloud.util.zyf.UuidUtil;
import com.xidian.zhiancloud.vo.monitor.MonitorDeviceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Summerless
 * @Date: 2021/5/14 20:23
 */
@RestController
@RequestMapping("/MonitorDevice")
public class MonitorDeviceController extends BaseController {
    @Autowired
    private MonitorDeviceService monitorDeviceService;
    @Autowired
    private MonitorDeviceTagService tagService;

    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO<Boolean> add(MonitorDevice monitorDevice) {
        monitorDevice.setSysid(getSessionUserCompanyId().toString());
        monitorDevice.setCreateTime(new Date(System.currentTimeMillis()));
        monitorDevice.setDeviceId(UuidUtil.generateShortUuid());
        Boolean res1 = monitorDeviceService.save(monitorDevice);
        MonitorDeviceTag tag = new MonitorDeviceTag();
        tag.setId(UuidUtil.generateShortUuid());
        tag.setFaId(monitorDevice.getDeviceId());
        Boolean res2 = tagService.save(tag);
        return ResultDTO.successOf(res1 && res2, GeneralResponseEnums.SUCCESS);
    }

    @PostMapping("/patch")
    public ResultDTO<Boolean> patch(MonitorDevice monitorDevice) {
        Boolean res = monitorDeviceService.updateById(monitorDevice);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    @GetMapping("/delete")
    public ResultDTO<Boolean> delete(@RequestParam("deviceId") String deviceId) {
        Boolean res = monitorDeviceService.removeById(deviceId);
        LambdaQueryWrapper<MonitorDeviceTag> query = Wrappers.<MonitorDeviceTag>lambdaQuery();
        query.eq(deviceId != null, MonitorDeviceTag::getFaId, deviceId);
        Boolean res1 = tagService.remove(query);
        return ResultDTO.successOf(res && res1, GeneralResponseEnums.SUCCESS);
    }

    @GetMapping("/getById")
    public ResultDTO<MonitorDeviceVO> getById(@RequestParam("deviceId") String deviceId) {
        MonitorDevice monitorDevice = monitorDeviceService.getById(deviceId);
        if (monitorDevice == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        MonitorDeviceVO vo = new MonitorDeviceVO();
        BeanUtils.copyProperties(monitorDevice, vo);

        LambdaQueryWrapper<MonitorDeviceTag> query = Wrappers.<MonitorDeviceTag>lambdaQuery();
        query.eq(vo.getDeviceId() != null, MonitorDeviceTag::getFaId, vo.getDeviceId());
        List<MonitorDeviceTag> monitorDeviceTags = tagService.list(query);
        vo.setTagList(monitorDeviceTags);
        vo.setAppID(monitorConfig.getAppID());
        vo.setAppKey(monitorConfig.getAppKey());
        vo.setMQTTaddr(monitorConfig.getMQTTaddr());
        vo.setMQTTport(monitorConfig.getMQTTport());
        vo.setMQTTSSLport(monitorConfig.getMQTTSSLport());
        return ResultDTO.successOf(vo, GeneralResponseEnums.SUCCESS);
    }

    @GetMapping("/getList")
    public ResultDTO<IPage<MonitorDevice>> getList(MonitorDeviceQO monitorDeviceQO) {
        if (monitorDeviceQO.getCurrent() == null || monitorDeviceQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        monitorDeviceQO.setCompanyId(getSessionUserCompanyId());
        LambdaQueryWrapper<MonitorDevice> query = Wrappers.<MonitorDevice>lambdaQuery();
        query.eq(monitorDeviceQO.getCompanyId() != null, MonitorDevice::getSysid, monitorDeviceQO.getCompanyId())
                .and(
                        Wrapper -> Wrapper.like(monitorDeviceQO.getKey() != null, MonitorDevice::getDeviceId, monitorDeviceQO.getKey())
                                .or().like(monitorDeviceQO.getKey() != null, MonitorDevice::getDeviceName, monitorDeviceQO.getKey())
                );
        IPage<MonitorDevice> page = new Page<MonitorDevice>(monitorDeviceQO.getCurrent(), monitorDeviceQO.getSize());
        return ResultDTO.successOf(monitorDeviceService.page(page, query), GeneralResponseEnums.SUCCESS);
    }


    @Autowired
    private MonitorConfig monitorConfig;

    @GetMapping("/getCode")
    public MonitorDeviceVO getCode(@RequestParam("deviceId") String deviceId) {
        MonitorDevice monitorDevice = monitorDeviceService.getById(deviceId);
        if (monitorDevice == null) {
            return null;
        }
        MonitorDeviceVO vo = new MonitorDeviceVO();
        BeanUtils.copyProperties(monitorDevice, vo);

        LambdaQueryWrapper<MonitorDeviceTag> query = Wrappers.<MonitorDeviceTag>lambdaQuery();
        query.eq(vo.getDeviceId() != null, MonitorDeviceTag::getFaId, vo.getDeviceId());
        List<MonitorDeviceTag> monitorDeviceTags = tagService.list(query);
        vo.setTagList(monitorDeviceTags);
        vo.setAppID(monitorConfig.getAppID());
        vo.setAppKey(monitorConfig.getAppKey());
        vo.setMQTTaddr(monitorConfig.getMQTTaddr());
        vo.setMQTTport(monitorConfig.getMQTTport());
        vo.setMQTTSSLport(monitorConfig.getMQTTSSLport());
        return vo;
    }

}
